package hui.com.controller.sales;

import hui.com.authority.UsersLogEntity;
import hui.com.model.res.sales.ResponeGetSalesForecast;
import hui.com.model.res.sales.ResponseGetSalesForecastList;
import hui.com.authority.Authority;
import hui.com.model.entity.sales.SalesForecastEntity;
import hui.com.model.req.sales.RequestGetLikeSalesForecast;
import hui.com.model.req.sales.RequestGetSalesForecastHistory;
import hui.com.model.res.MyResponse;
import hui.com.service.SalesForecastService;
import hui.com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static java.lang.System.out;

@CrossOrigin(origins = "*")//处理跨源资源共享
@RequestMapping("/public/sales")
@Controller
public class salesForecast {
    @Autowired
    @Resource
    SalesForecastService salesForecastService;



    /**
     *  全局模糊查询
     * @param sales 里面都可用
     * @return RequestGetLikeSales
     */
    @RequestMapping("/salesForecastShow")
    @ResponseBody
    @Authority("sales_show")
    public Object showSalesForecastList(@RequestBody(required = false) RequestGetLikeSalesForecast sales){

        if (sales==null){
            sales = new RequestGetLikeSalesForecast();
        }


        ResponseGetSalesForecastList salesRespone =  salesForecastService.getLikeListSalesForecast(sales);

        return MyResponse.builder().code(200).data(salesRespone).build();

    }

    /**
     * 通过销售预测编号查询指定销售预测订单，同时展示用户。
     * @param sales 里面的salesCode 才有效果
     * @return ResponeGetSales
     */
    @RequestMapping("/getSalesForecast_User")
    @ResponseBody
    @Authority("sales_show")
    public Object getSalesForecast_User(@RequestBody(required = false) SalesForecastEntity sales){

        ResponeGetSalesForecast salesRes = salesForecastService.getSalesForecast_User(sales);

        return MyResponse.builder().code(200).data(salesRes).build();

    }


    /**
     * 新增：前置条件，需要有指定编号。接口为salesADD_getSalesCode
     * @param sales sales
     * @return boolean
     */
    @PostMapping("/salesForecastADD")
    @ResponseBody
    @Authority("sales_add")
    public Object salesForecastAdd (@RequestBody SalesForecastEntity sales){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        UsersLogEntity usersLog = RedisUtil.getValue(request.getHeader("Authorization"));

        sales.setUID(usersLog.getUID());

        sales.setSalesForecastCode(salesForecastService.salesForecastADD_getSalesCode());

        // 查看用户是否存在，不存在则记录
        if (!salesForecastService.getCustNameExist(sales.getCustName())){
            salesForecastADD_addCustName(sales.getCustName());
        }
        boolean a = salesForecastService.addSaleForecast(sales);

        return MyResponse.builder().code(200).data(a).build();

    }


    /**
     * 更新销售预测记录
     * @param sales sales
     * @return boolean
     */
    @PostMapping("/salesForecastUPD")
    @ResponseBody
    @Authority("sales_upd")
    public Object salesForecastUPD (@RequestBody SalesForecastEntity sales){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        UsersLogEntity usersLog = RedisUtil.getValue(request.getHeader("Authorization"));

        out.println(sales);
        // 存储到历史记录
        SalesForecastEntity salesHistory = salesForecastService.getSalesForecast(sales);
        salesForecastService.addHistorySaleForecast(salesHistory);
        // 更新
        sales.setUpdUID(usersLog.getUID());//记录当前更新人
        sales.setUID(salesHistory.getUID());// 制单人还是原来的制单人
        Integer a = salesForecastService.updateSalesForecast(sales);

        return MyResponse.builder().code(200).data(a).build();

    }

    /**
     *  指定code模糊查询历史记录
     * @param sales code可用
     * @return RequestGetLikeSales
     */
    @RequestMapping("/salesForecastHistoryShow")
    @ResponseBody
    @Authority("sales_show")
    public Object showSalesForecastHistoryList(@RequestBody(required = false) RequestGetSalesForecastHistory sales){
        ResponseGetSalesForecastList responseGetSalesForecastList = salesForecastService.getSalesForecastHistoryList_User(sales);

        return MyResponse.builder().code(200).data(responseGetSalesForecastList).build();

    }

    /**
     * 指定历史ID 查询单个历史记录
     * @param sales 里面的salesHistoryID 才有效果
     * @return ResponeGetSales
     */
    @RequestMapping("/getSalesForecastHistory_User")
    @ResponseBody
    @Authority("sales_show")
    public Object getSalesForecastHistory_User(@RequestBody(required = false) RequestGetSalesForecastHistory sales){

        ResponeGetSalesForecast salesRes = salesForecastService.getSalesForecastHistory_User(sales);

        return MyResponse.builder().code(200).data(salesRes).build();

    }

    /**
     * 新增前的功能之一，查询可以使用的编号
     * @return 准备用于新增的编号
     */
    @PostMapping("/salesForecastADD_getSalesCode")
    @ResponseBody
    @Authority("sales_add")
    public Object salesForecastADD_getSalesCode (){
        return MyResponse.builder().code(200).data(salesForecastService.salesForecastADD_getSalesCode()).build();
    }

    /**
     * 查询作用于增改，模糊查询客户
     * @param custName 客户名称
     * @return 数组
     */
    @RequestMapping("/salesForecastADD_getCustName")
    @ResponseBody
    @Authority("sales_add")
    public Object salesForecastADD_getCustName (@RequestBody(required = false) String custName){

        out.println("aaaaaaaa:"+custName);
        String[] a = salesForecastService.getCustNameArray(custName);
        return MyResponse.builder().code(200).data(a).build();

    }

    /**
     * 查询作用于增改，新增客户
     * @param custName 客户名称
     * @return true/false
     */
    @PostMapping("/salesForecastADD_addCustName")
    @ResponseBody
    @Authority("sales_add")
    public Object salesForecastADD_addCustName (@RequestBody String custName){
        if (custName.isEmpty()){
            return MyResponse.builder().code(201).build();
        }
        boolean a = salesForecastService.addCustName(custName);
        return MyResponse.builder().code(200).data(a).build();

    }

}
